package com.mz.ozy.fragment;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mz.ozy.R;
import com.mz.ozy.activity.me.ObShopActivity;
import com.mz.ozy.activity.shop.CarActivity;
import com.mz.ozy.activity.shop.ShopDetailActivity;
import com.mz.ozy.adapter.ObShopAdapter;
import com.mz.ozy.base.BaseFragment;
import com.mz.ozy.bean.EventBean;
import com.mz.ozy.bean.ObShopBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.ObShopFragmentPersenter;
import com.mz.ozy.mvp.view.ObShopFragmentContract;
import com.mz.ozy.utils.Utils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author wansong
 * @description: 欧花商城分类
 * @date :2020/9/24 17:22
 */
public class ObShopFragment extends BaseFragment<ObShopFragmentContract.View, ObShopFragmentPersenter> implements ObShopFragmentContract.View {

    @BindView(R.id.recyclerview)
    RecyclerView rv_ob_shop;
    //        @BindView(R.id.rf_ob_shop)
//        SmartRefreshLayout rf_ob_shop;
    @BindView(R.id.ll_no_data)
    LinearLayout ll_no_data;
//    @BindView(R.id.dragview)
//    ImageView mDragview;
//    @BindView(R.id.rele)
//    RelativeLayout rele;

    private ObShopAdapter adapter;
    private List<ObShopBean.PBean.PageItemsBean> mBeanList = new ArrayList<>();
    private int tag=0;
    private int page = 1;

    private View rooView;
    //0正序1倒序
    private int orderType = 0;
    //排序0綜合1銷量2价格
    private int orderBy = 0;
    //查询类型0根据商品分类1搜索2推荐
    private int type = 0;
    //目录ID|搜索内容
    private String content = new String();

    private float mOriginalX, mOriginalY;//手指按下时的初始位置
    private float lastX, lastY; //最后一次触摸事件的坐标
    private int containerWidth, containerHeight;    //容器宽高

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        if (rooView != null) {
            return rooView;
        }
        rooView = inflater.inflate(R.layout.fragment_ob_shop, container, false);
        ButterKnife.bind(this, rooView);
//        initView();
        //initLottery();
        return rooView;
    }

//    private void initLottery() {
//        rele.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
//            @Override
//            public boolean onPreDraw() {
//                //不能直接在onCreate()方法中得到宽高,会得到0,所以注册视图树的观察者来得到宽高
//                //OnPreDrawListener是当一个视图树将要绘制时，所要调用的回调函数的接口类
//                containerWidth = rele.getWidth();
//                containerHeight = rele.getHeight();
//                return true;
//            }
//        });
//
////        mDragview.setOnTouchListener(new View.OnTouchListener() {
////            @Override
////            public boolean onTouch(View view, MotionEvent motionEvent) {
////                switch (motionEvent.getAction()) {
////                    case MotionEvent.ACTION_DOWN:
////                        //记录触摸时的坐标,这里为什么要用getRawX()和getRawY()相信理解getX(),getY()和getRawX(),getRawY()的区别就知道为什么了
////                        lastX = motionEvent.getRawX();
////                        lastY = motionEvent.getRawY();
////                        mOriginalX = motionEvent.getRawX();
////                        mOriginalY = motionEvent.getRawY();
////                        //return true对事件进行拦截,不继续下发,防止继续响应onClick事件.
////                        return true;
////                    case MotionEvent.ACTION_MOVE:
////                        //每次移动的距离
////                        float distanceX = motionEvent.getRawX() - lastX;
////                        float distanceY = motionEvent.getRawY() - lastY;
////
////                        //控件将要移动到的位置,先计算一下,不在ofFloat()方法中再计算是因为要防止控件移动到容器之外.
////                        float nextX = mDragview.getX() + distanceX;
////                        float nextY = mDragview.getY() + distanceY;
////                        //如果将要移动到的 x 轴坐标小于0,则等于0,防止移出容器左边
////                        if (nextX < 0)
////                            nextX = 0;
////                        //防止移出容器右边
////                        if (nextX > containerWidth - mDragview.getWidth())
////                            nextX = containerWidth - mDragview.getWidth();
////                        //防止移出容器顶边
////                        if (nextY < 0)
////                            nextY = 0;
////                        //防止移出容器底边
////                        if (nextY > containerHeight - mDragview.getHeight())
////                            nextY = containerHeight - mDragview.getHeight();
////                        //利用属性动画改变控件的x,y坐标
////                        ObjectAnimator mObjectAnimatorX = ObjectAnimator.ofFloat(mDragview, "x", mDragview.getX(), nextX);
////                        ObjectAnimator mObjectAnimatorY = ObjectAnimator.ofFloat(mDragview, "y", mDragview.getY(), nextY);
////                        AnimatorSet mAnimatorSet = new AnimatorSet();
////                        mAnimatorSet.playTogether(mObjectAnimatorX, mObjectAnimatorY);
////                        mAnimatorSet.setDuration(0);
////                        mAnimatorSet.start();
////                        //移动完之后记录当前坐标
////                        lastX = motionEvent.getRawX();
////                        lastY = motionEvent.getRawY();
////                        break;
////                    case MotionEvent.ACTION_UP:
//////                        if (Math.abs(motionEvent.getRawX() - mOriginalX) <
//////                                Utils.dp2px(rooView, 3) &&
//////                                Math.abs(motionEvent.getRawY() - mOriginalY) <
//////                                        Utils.dp2px(rooView.this, 3)) {
//////                            CarActivity.go(rooView.this);
//////                        }
////                        break;
////                }
////                return false;
////            }
////        });
//    }

    public ObShopFragment(int type) {
        this.type = type;
    }

    public int getType() {
        return type;
    }

    //获取列表数据
    public void initData() {
//        if (mPresenter == null) return;
//        if (type == 2) {
//            mPresenter.getList(getActivity(), page + "", orderBy + "", type + "", content, orderType + "");
//        } else if (type == 1) {
//            mPresenter.getList(getActivity(), page + "", orderBy + "", type + "", content, orderType + "");
//        } else {
//            mPresenter.getList(getActivity(), page + "", orderBy + "", 0 + "", type + "", orderType + "");
//        }
//        KLog.e("tag", "page=" + page + ",orderBy=" + orderBy + ",type=" + type + ",content=" + content + ",orderType=" + orderType);
    }

    private void initView() {

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
        rv_ob_shop.setLayoutManager(gridLayoutManager);
        rv_ob_shop.setNestedScrollingEnabled(false);
        adapter = new ObShopAdapter(mBeanList);
        rv_ob_shop.setAdapter(adapter);

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                //商品详情
                final ObShopBean.PBean.PageItemsBean item = (ObShopBean.PBean.PageItemsBean) adapter.getItem(position);
                switch (view.getId()) {
                    case R.id.ll_item_view:
                        Intent intent = new Intent(getActivity(), ShopDetailActivity.class);
                        intent.putExtra("goodsId", item.getId() + "");
                        intent.putExtra("tag", Constants.OB_TYPE);
                        startActivity(intent);
                        break;
                    case R.id.iv_search_car: {
                        Intent intentP = new Intent(getActivity(), ShopDetailActivity.class);
                        intentP.putExtra("goodsId", item.getId() + "");
                        intentP.putExtra("show", 1);
                        intentP.putExtra("tag", Constants.OB_TYPE);
                        startActivity(intentP);
                        break;
                    }
                }
            }
        });

//        rf_ob_shop.setOnRefreshListener(new OnRefreshListener() {
//            @Override
//            public void onRefresh(RefreshLayout refreshlayout) {
//                tag = 0;
//                page = 1;
//                //下拉充值数据
//                content = "";
//                initData();
////                mPresenter.getList(getActivity(), page + "", orderBy + "", type + "", content, orderType + "");
//            }
//        });

//        rf_ob_shop.setEnableAutoLoadmore(true);
//
//        rf_ob_shop.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
//            @Override
//            public void onLoadmore(RefreshLayout refreshlayout) {
//                tag = 1;
//                page = page + 1;
//                initData();
////                mPresenter.getList(getActivity(), page + "", orderBy + "", type + "", content, orderType + "");
//            }
//
//            @Override
//            public void onRefresh(RefreshLayout refreshlayout) {
//                tag = 0;
//                page = 1;
//                //下拉重置数据
//                content = "";
//                initData();
////                mPresenter.getList(getActivity(), page + "", orderBy + "", type + "", content, orderType + "");
//            }
//        });

        initData();
    }

    @Override
    public void getListSuccess(ObShopBean bean) {
        Log.e("TAG", bean.getSessionId());

        if (tag == 0) {
            EventBus.getDefault().post(new EventBean(Constants.EventCode.OB_SHOP_BACK1, null));
//            rf_ob_shop.setLoadmoreFinished(false);
//            rf_ob_shop.finishRefresh();
            mBeanList.clear();
            if (bean.getP().getPageItems().size() == 0) {
                ll_no_data.setVisibility(View.VISIBLE);
                rv_ob_shop.setVisibility(View.GONE);
            } else {
                ll_no_data.setVisibility(View.GONE);
                rv_ob_shop.setVisibility(View.VISIBLE);
            }
        } else if (tag == 1) {
            EventBus.getDefault().post(new EventBean(Constants.EventCode.OB_SHOP_BACK2, null));
//            rf_ob_shop.finishLoadmore();
        }
        mBeanList.addAll(bean.getP().getPageItems());
        Log.e("mBeanList", "=========" + mBeanList.size());

        adapter.notifyDataSetChanged();
        if (bean.getP().getPageItems().size() == 0 || bean.getP().getPageSize() * bean.getP().getPageNumber() >= bean.getP().getPageTotle()) {
            EventBus.getDefault().post(new EventBean(Constants.EventCode.OB_SHOP_BACK3, null));
//            rf_ob_shop.setLoadmoreFinished(true);//将不会再次触发加载更多事件
        }
    }

    //排序和搜索
    public void sortAndSearchData(int type, int orderBy, String content, int orderType) {
        this.type = type;
        this.orderBy = orderBy;
        this.content = content;
        this.orderType = orderType;
        initData();
    }

    //主页刷新
    public void changeRefresh() {
        tag = 0;
        page = 1;
        //下拉重置数据
        content = "";
        initData();
    }
}
